package com.manage.enter.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.jdesktop.databuffer.*;
import org.jdesktop.databuffer.json.DataSet2Json;
import org.jdesktop.databuffer.json.Json2DataSet;

import com.manage.enter.service.EnterService;
import com.manage.enter.service.impl.EnterServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EnterAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private EnterService service;
	private String username;
	private String password;

	public EnterService getService() {
		return service;
	}

	public String responseJson;

	public String getResponseJson() {
		return responseJson;
	}

	public void setResponseJson(String responseJson) {
		this.responseJson = responseJson;
	}

	private String args = "";

	public void setArgs(String args) {
		this.args = args;
	}

	public String getArgs() {
		return args;
	}

	public void setService(EnterService service) {
		this.service = service;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		ActionContext context=ActionContext.getContext();  
		String k  = (String)context.getParameters().get("args");
		DataSet ds = service.getData(null);
		
		HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);   

		String result = DataSet2Json.toJsonString(ds);

		request.getParameterMap();
		Map map = new HashMap();
		//DataSet2Json.toJson(ds).putAll(map);
		//DataSet2Json.toJson(ds);
		
		DataSet ds2 = Json2DataSet.parse(result);
		ds2.toString();
		DataSet2Json.toJsonString(ds2);
		this.setResponseJson(result);
		return "success";
		// return "login";
	}

	public String login() throws Exception {

		service = new EnterServiceImpl();

		return service.load(this.username, this.password) ? "success" : "error";
	}
}